    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.logic;

import java.awt.event.*;
import java.util.HashSet;

import javax.swing.event.*;

import ch.zarzu.champions.builder.gui.*;
import ch.zarzu.champions.builder.*;

public class Listener implements ActionListener, HyperlinkListener {
    private static Listener instance = null;
    
    private MainInterface user_interface;
    private BuildUpdater updater;
    private Main main;
    
    private Listener() {}
    
    //button events
    public void actionPerformed(ActionEvent a) {
        user_interface = MainInterface.getInstance();
        updater = BuildUpdater.getInstance();
        main = Main.getInstance();
        
        System.out.println("action: " + a.getActionCommand());
        
        if(a.getActionCommand().equals("new")) {
            updater.clearEverything();
            PrefLink.getInstance().updateLastSaved("");
            user_interface.setChanged(false);
            
        } else if(a.getActionCommand().equals("saveas")) {
            main.saveBuildAs();
        
        } else if(a.getActionCommand().equals("save")) {
            main.saveBuild();
        
        }else if(a.getActionCommand().equals("open")) {
            main.openBuild();

        }else if(a.getActionCommand().equals("print")) {
            Printer.getInstance().printOut();
        
        } else if(a.getActionCommand().endsWith("export")) {
            updater.exportBuild(a.getActionCommand().substring(0, a.getActionCommand().length() - 6));
            
        } else if(a.getActionCommand().equals("help")) {
            user_interface.openHelp();
        
        } else if(a.getActionCommand().equals("about")) {
            user_interface.openAbout();
        
        } else if(a.getActionCommand().equals("backtomain")) {
            user_interface.backToMain();
        
        } else if(a.getActionCommand().equals("update")) {
            main.startUpdate();
            
        } else if(a.getActionCommand().equals("download")) {
            main.startDownload();
            
        }else if(a.getActionCommand().equals("exit")) {
            main.exit();
            
        } else if(a.getActionCommand().startsWith("language:")) {
            PrefLink.getInstance().setPreference("language", a.getActionCommand().substring(9));
            
            user_interface.openInfo();
            
            LanguageUpdateThread lang_thread = new LanguageUpdateThread();
            lang_thread.start();
            
            
            //user_interface.backToMain();
            
            user_interface.setTopText(SystemLink.getInstance().translate("changed_language"), 0);
        }
    }
    
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            Main.getInstance().showInBrowser(e.getURL().toString());
        }
    }
    
    public void selectPools(HashSet<String> pool_ids) {
        HashSet<String> set = new HashSet<String>();
        for(String pool_id : pool_ids) {
            set.add("pool:" + pool_id);
        }
        BuildUpdater.getInstance().clearPoolFilter("powers");
        BuildUpdater.getInstance().extendFilter("powers", set);
        BuildUpdater.getInstance().enforceFilter();
    }
    
    public void addPools(HashSet<String> pool_ids) {
        HashSet<String> set = new HashSet<String>();
        for(String pool_id : pool_ids) {
            set.add("pool:" + pool_id);
        }
        BuildUpdater.getInstance().extendFilter("powers", set);
        BuildUpdater.getInstance().enforceFilter();
    }
    
    public void startSearch(String search) {
        HashSet<String> set = new HashSet<String>();
        BuildUpdater updater = BuildUpdater.getInstance();
        
        boolean in_quotes = false;
        String parsed_s = "";
        for(int i = 0; i < search.length(); i++) {
            char c = search.charAt(i);
            
            if(c == '"') {
                in_quotes = !in_quotes;
            } else if(c == ' ' && in_quotes) {
                parsed_s += "#";
            } else {
                parsed_s += c;
            }
        }
        
        String[] split = parsed_s.replace(" and ", "+").split("[ ]");
        
        for(String s : split) {
            String final_string = "";
            for(String ind_s : s.split("[+]")) {
                if(!ind_s.startsWith("effect:") && !ind_s.startsWith("damage:") && !ind_s.startsWith("buff:")) {
                    final_string += "text:" + ind_s.replace("#", " ").trim();
                } else {
                    final_string += ind_s.replace("#", " ").trim();
                }
                final_string += "+";
            }
            final_string = final_string.substring(0, final_string.length() - 1);
            set.add(final_string);
            
            System.out.println("search query: " + final_string);
        }
        
        updater.clearSearchFilter("powers");
        updater.extendFilter("powers", set);
        updater.enforceFilter();
        updater.forceUpdateDescription();
    }

    public void powerMouseOver(String id) {
        BuildUpdater.getInstance().updateDescription(id, false);
    }
    
    public void powerClick(String id) {
        BuildUpdater.getInstance().addRemovePower(id);
    }
    
    public void advantageClick(String id) {
        BuildUpdater.getInstance().addRemoveAdvantage(id);
    }
    
    public void frameworkClick(String id) {
        BuildUpdater.getInstance().addRemoveFramework(id);
    }
    
    public void powerLock(String id) {
        BuildUpdater.getInstance().lockDescription(id);
    }
    
    public void buildPowerClick(BuildPower bp) {
        updater = BuildUpdater.getInstance();
        
        updater.getActiveSlot().setActive(false);
        bp.setActive(true);
        
        updater.setFilterType("powers");
        
        updater.enforceFilter();
    }
    
    public void buildPowerActivated(BuildPower bp) {
        bp.setPowerActivated(!bp.isPowerActivated());
        MainInterface.getInstance().setChanged(true);
    }
    
    public void buildPowerSwitchPool(BuildPower bp) {
        BuildUpdater updater = BuildUpdater.getInstance();
        MainInterface user_interface = MainInterface.getInstance();
        HashSet<String> set = new HashSet<String>();
        
        HashSet<String> pool_ids = SystemLink.getInstance().getPoolsById(bp.getId());
        
        user_interface.clearPoolButtons();
        for(String pool_id : pool_ids) {
            if(pool_id.length() == 2) {
                set.add("pool:" + pool_id);
                user_interface.setPool(pool_id);
            }
        }
        
        
        
        updater.clearPoolFilter("powers");
        updater.extendFilter("powers", set);
        updater.enforceFilter();
    }
    
    public void bubblesClick(BuildPower bp) {
        BuildUpdater.getInstance().setFilterType("advantages");
        
        HashSet<String> set = new HashSet<String>();
        set.add("power:" + bp.getId());
        updater.clearPowerFilter("advantages");
        updater.extendFilter("advantages", set);
        updater.enforceFilter();
    }
    
    public void LanguageUpdateDone() {
        user_interface.backToMain();
    }
    
    public static Listener getInstance() {
        if(instance == null)
            instance = new Listener();
        return instance;
    }
}
